#ifndef __POD_SERVER_INTERFACE__
#define __POD_SERVER_INTERFACE__

#include <vector>
#include "SocketClient.h"
#include "Thread.h"
#include "protocol.h"
#include "Constants.h"

#define ADDRESS "137.222.103.92"
#define TCP_PORT 2006

// number of elements in smoothing array of joystick positions  
#define SMOOTH_SIZE (80)
// time between sending move commands (seconds)
#define MOVE_INTERVAL (0.05)
// max value of x, y, z, r, p or yaw for pod movement
#define MAX_MOVEMENT (0.6)
// rate at which pod movements return to the middle
#define POD_MOVEMENT_DECAY (1)
// factor for multiplying x, y and z by
#define POD_SENSITIVITY (0.01)

typedef struct {
    Ogre::Vector3 acceleration;
    float time_ago;
} pod_position;

/*
 * Object to mediate communication with the pod server
 */
class PodServerInterface
{
    public:
        PodServerInterface(void);
       ~PodServerInterface(void);
        PodServerInterface(const PodServerInterface&);
        PodServerInterface& operator=(PodServerInterface&);
        
        float getJoystickX();
        float getJoystickY();
        bool getThumbButt();
        bool getTriggerButt();
        void simulateAcceleration(Ogre::Vector3 acceleration);
        void setRollAndPitch(float r, float p);
        void home();
        void stop();
        
        // time taken for pod movements to return to middle (bigger means longer)
        static float POD_MOVEMENT_DELAY;
        // maximum magnitude of acceleration on the ball after gravity
        static float MAX_BALL_ACCELERATION;
        static float P_OR_R_SPEED_CHANGE;
        static float P_OR_R_TIME;
        
    private:
        static double last;
        static void pollInputs();
        static void sendMove();
        static std::vector<pod_position> pod_positions;
        static polldata pdata;
        static movedata mdata;
        static SocketClient *moveClient, *pollClient;
        SocketClient *commandClient;
        bool command_connected;
        static bool poll, move, moved;
        static bool move_connected, poll_connected;
        static int index; // to place the joystick values in the smooth array
        static float **joySmooth, joyXtotal, joyYtotal, joyX, joyY, thumbButt, trigger;
        static float constrainMovement(float);
        static float pitch_speed, roll_speed;
        static int processJoysickXValue(int);
        static int processJoysickYValue(int);
        static void moveLoop(void *);
        static void pollLoop(void *);
        static int pollCount, moveCount;
};

#endif
